#include "s3c2440_soc.h"

void timer_irq(void)
{
    /* 点灯计数 */
    static int cnt = 0;
    int tmp;

    cnt ++;

    tmp = ~cnt;
    tmp &= 7;
    GPFDAT &= ~(7<<4);
    GPFDAT |= (tmp<<4);
}

void timer_init(void)
{
    /*
     *     设置TIMER0的时钟
     *     Timer clk = PCLK / prescaler value+1 / divider value
     *               = 5000 0000 /(99 + 1)/16
     *               = 31250
     */
    TCFG0 = 99; //prescaler 0 = 99 用于timer 0,1
    TCFG1 &= ~0xf;
    TCFG1 |= 3; //MUX0 : 1/16

    /* 设置TIMER0 的初始值 */
    TCNTB0 = 15625; //0.5s 中断一次

    /* 加载初始值, 启动timer 0 */
    TCON |= (1<<1); //update from TCNTB0 & TCMPB0
    
    /* 设置为自动加载并启动 */
    TCON &= ~(1<<1);
    TCON |= (1<<0) | (1<<3); //bit0:start, bit3: auto reload

    /* 设置中断 */
    register_irq(10,timer_irq);
}
